<template>
    <div class="all">
        <div class="canvas">
            <Earth></Earth>
        </div>
        <div class="login">
            <div class="left"></div>
            <div class="right">
                <div class="head">
                    <h1>{{ sign1 }}:)&nbsp &nbsp &nbsp</h1>
                    <span>Everything Happens for a Reason.</span>
                </div>
                <div class="form">
                    <div class="username f">
                        <div>用户名</div>
                        <div class="input">
                            <el-input v-model="userName" clearable :prefix-icon="Avatar" />
                        </div>
                    </div>
                    <div class="password f">
                        <div>密码</div>
                        <div class="input">
                            <el-input v-model="passWord" type="password" show-password :prefix-icon="Lock" />
                        </div>
                    </div>
                    <div class="verification f">
                        <div>验证码</div>
                        <div class="input">
                            <el-input v-model="verifiCation" />
                        </div>
                    </div>
                    <div class="yzmImg">
                        <YZM :change="change_img_code" @click="changeImg" @getCode="getCode"></YZM>
                    </div>

                </div>
                <div class="btn">
                    <el-button type="primary" size="large" @click="sign">&nbsp &nbsp &nbsp &nbsp  登录 &nbsp &nbsp
                        &nbsp &nbsp </el-button>
                    <a href="https://www.baidu.com" class="toot">忘记密码</a>
                </div>
            </div>
        </div>
    </div>


</template>

<!------------------------------>

<script setup lang="ts">
import Earth from '../components/earth.vue'
import YZM from '../components/verification-code.vue'
import { Lock ,Avatar } from "@element-plus/icons-vue"
//@ts-ignore
import Md5 from 'md5'

//所有需要实例化生成的都不能写链式,否则会失效!!!!!!
const global = getCurrentInstance()
const router = useRouter()

let sign1 = computed(() => {
    let sign
    switch (new Date().getHours()) {
        case 6 :
        case 6:
        case 7:
        case 8:
        case 9:
        case 10:
        case 11:
            sign = "早上好"
            break;
        case 12:
        case 13:
            sign = "中午好"
            break;
        case 14:
        case 15:
        case 16:
        case 17:
            sign = "下午好"
            break;
        default:
            sign="晚上好"
    }
    return sign
})
//
let userName = ref<string>('')
let passWord = ref<string>('')
let verifiCation=ref<string>('')

//验证码
let ImgCode:string
let change_img_code=ref(false)
const changeImg = () => {
    change_img_code.value=!change_img_code.value
}
const getCode = (code :string) => {
    ImgCode = code
}
//登录
const sign = async () => {
    if (!verifiCation.value) {
        ElMessage.warning({ message: '没填验证码啊喂' })
        return
    }
    if (Md5(verifiCation.value) != ImgCode)
    {
        ElMessage.warning({
            message: '验证码有误',
            grouping: true,
            center:true
        })
        changeImg()
        return
    } 
    if (!userName.value || !passWord.value) {
        ElMessage.warning({
            message:'信息未填全'
        })
        changeImg()
        return
    }
    const loginData = {
        username: userName.value,
        password:passWord.value
    }
    const data = await global?.proxy?.$axios.post('/login', loginData)
    //token存储
    if (data) {
        ElMessage.success({message:`欢迎回来! ${loginData.username}`})
        sessionStorage.setItem('token', data.data?.data?.token)
        sessionStorage.setItem('adminname', loginData.username)
        sessionStorage.setItem('role', data.data?.data?.role)
        router.replace('/main')
    }
}


</script>
<!------------------------------>

<style scoped lang="scss">

.canvas{
    position: relative;
    left: 4rem;
}

.all{
    min-width: 100vw;
    min-height: 100vh;
    display: flex;
    padding-right: 4rem;
    margin: 0px;
    align-items: center;
    justify-content: space-between;
    background-image: url('../assets/img/login_background.jpg');
}
.login{
    box-shadow: 3px -3px 15px rgba(125,125,125,0.3) ;
    border-radius:.625rem /* 10px -> .625rem */;
    display: flex;
    align-items: center;
    opacity: 1;
    background-color: rgba(255, 255, 255, 0.91);
    .left{
        height: 37.5rem /* 600px -> 37.5rem */;
        width: 31.25rem /* 500px -> 31.25rem */;
        background-image: url('../assets/svg/login-bg1.svg');
        background-repeat: no-repeat;
        background-position: 50%;
    }
    .right{
        display: flex;
        flex-direction: column;
        align-items: center;
        padding-top: 3rem;
        width:21.875rem /* 350px -> 21.875rem */;
        height:37.5rem /* 600px -> 37.5rem */;
        .head{
            cursor: default;
        h1 {
                font-size: 3rem;
                font-family: 'KaiTi';
            }
        }
        .form{
            width: 100%;
            padding-top: 1.5rem;
            .f{
                margin-bottom: 1.5rem;
                display: flex;
                padding-left: 1rem;
                justify-content: space-between;
                align-items: center;
                padding-right: 2rem;
                padding-left: 1.8rem;
                .input{
                    width: 12.5rem /* 200px -> 12.5rem */;
                }
            }
        }
        .btn{
            display: flex;
            align-items: center;
            padding-top:1.5rem;
            padding-left: 4rem;
            .toot{
                padding-top: 1.2rem;
                display: inline-block;
                padding-left: 0.4rem;
            }
        }
    }
}
.yzmImg{
    margin-top: -0.8rem;
    width: 7rem;
    position: relative;
    left: 7.5rem;
}


</style>